const cloud = require('wx-server-sdk')
cloud.init({
  env: 'cake-developer-oqn76'
})
exports.main = async (event, context) => {

  const wxContext = cloud.getWXContext()

  var scene = event.scene;
  var page = event.page;
  var width = event.width;

  try {
    //  1、

    //  1、通过云调用生成二维码
    const result = await cloud.openapi.wxacode.getUnlimited({
      scene: scene,
      page: page,
      width: width
    });

    //  2、上传图片到云存储中
    const upload = await cloud.uploadFile({
      cloudPath: scene + '.jpg',
      fileContent: result.buffer
    });

    //  3、返回图片地址
    var fileID = upload.fileID;

    console.log("fileId=" + fileID);

    const fileList = [fileID];
    const imgList = await cloud.getTempFileURL({
      fileList: fileList
    })
    return imgList.fileList;
  } catch (err) {
    console.log(err)
    return err
  }
}